"""控制爪子开合...
"""
import threading
import DobotDllType as dType

# def clawCtrl(api, pos):
#   t = 1
#   dType.SetIOMultiplexingEx(api, 4, 2, 1)
#   for count in range(int(t)):
#     dType.SetIOPWMEx(api, 4, 50, pos, 1)
#     dType.SetWAITCmdEx(api, 0.1, 1)
#
#
# """控制爪子开合...
# """
# def clawOpen(api):
#   clawCtrl(api, 8)
#
#
# """控制爪子开合...
# """
# def clawClose(api):
#   clawCtrl(api, 11)
from DobotApi import DobotApi
from singleton import singleton
from time import sleep

@singleton
class Gripper(object):
    def __init__(self):
        self.pre_pos = 40

        pass

    def claw_ctrl(self, pos):
        dType.SetIOMultiplexingEx(DobotApi().instance(), 4, 2, 1)
        rev = -1
        if pos > self.pre_pos:
            rev = 1
        for p in range(self.pre_pos, pos + rev, rev):
            dType.SetIOPWMEx(DobotApi().instance(), 4, 200, p, 1)
            dType.SetWAITCmdEx(DobotApi().instance(), 0.1, 1)

        self.pre_pos = pos

    def open(self):
        self.claw_ctrl(38)

    def close(self):
        self.claw_ctrl(45)

    def do_action(self, params, action):
        if isinstance(params, bool):
            if params is True:
                self.open()
            else:
                self.close()
        return True

    def wait_action(self, params):
        sleep(1)
        return True
